var webSocket =
    new WebSocket('ws://localhost:1818');

window.onbeforeunload = function () {
};
window.onbeforeunload=function(){
    closeUser();
}
webSocket.onerror = function (event) {
    onError(event);
};

webSocket.onopen = function (event) {
    onOpen(event);
};

webSocket.onclose = function (event) {
    onClose(event);
};

webSocket.onmessage = function (event) {
    onMessage(event)
}
var begin_data = "data:image/jpeg;base64,";

function onMessage(event) {
    if (event.data.indexOf('BeginAfterSign') >= 0) {
        var newLogMsg = event.data.replace('BeginAfterSign', '').replace('EndAfterSign', '') + "\n";
        readSignCallBack(newLogMsg);
        $(".loadingShade").fadeOut();
    }
    else if (event.data.indexOf('BeginFinishInfo') >= 0) {
        var newLogMsg = event.data.replace('BeginFinishInfo', '').replace('EndFinishInfo', '') + "\n";
        alert(newLogMsg);
    }
    else if (event.data.indexOf('BeginFinishEvaluate') >= 0) {
        var newLogMsg = event.data.replace('BeginFinishEvaluate', '').replace('EndFinishEvaluate', '') + "\n";
        alert(newLogMsg);
    }

}

function onError(event) {
    alert(event.data);
}

function onClose(event) {

}

function onOpen(event) {
}

function start(el) {
    webSocket.send(el);
    return false;
}

var readCardCallBack;
function readCard() {
    $(".loadingShade").fadeIn();
    $("#certFront").show();
    $("#certBack").show();
    new Device().startFun(function (data) {

        var height = $("#certFront").height();
        var width = $("#certFront").width();
        var cloneDom1 = $("#certFront").clone(true);
        var cloneDom2 = $("#certBack").clone(true);
        cloneDom1.css({
            "background-color": "white",
            "position": "absolute",
            "top": "0px",
            "z-index": "-1",
            "width": width,
            "height": height
        });
        cloneDom2.css({
            "background-color": "white",
            "position": "absolute",
            "top": "0px",
            "z-index": "-1",
            "width": width,
            "height": height
        });
        $("body").append(cloneDom1);
        $("body").append(cloneDom2);

        $("#name").text(data.name);
        $("#certCode").text(data.certCode);
        $("#telephone").text(data.telephone);
        $("#add").text(data.add);
        $("#schoolName").text(data.schoolName);
        $("#stuType").text(data.stuType);
        $("#amt").text(data.amt+"元/人");
        $("#insuranceInfo").children().hide();
        $("#"+data.insurer).show();

        html2canvas(cloneDom1[0]).then(function (canvas) {
            data.frontImg = canvas.toDataURL('image/jpeg', 1.0);
            readCardCallBack(data);
            $(".loadingShade").fadeOut();
        });
        html2canvas(cloneDom2[0]).then(function (canvas) {
            data.backImg = canvas.toDataURL('image/jpeg', 1.0);
            readCardCallBack(data);
            $(".loadingShade").fadeOut();
        });


        // var interval=setInterval(function () {
        //     if(data.backImg!=null&& data.frontImg!=null){
        //         readCardCallBack(data);
        //         $(".loadingShade").fadeOut();
        //         clearInterval(interval);
        //     }
        // },300)
        // setTimeout(function () {
        //     clearInterval(interval);
        //     $(".loadingShade").fadeOut();
        // },5000);

    });
}

var readSignCallBack;
function readSign() {
    $(".loadingShade").fadeIn();
    start('OpenSign(d:\\1.xml)');
}




function openUser(data) {
    var urlBase=window.location.href;
    urlBase=urlBase.substr(0,7+urlBase.substr(7).indexOf("/"));
    var url=urlBase+"/views/user/index.html?"+SetUrlParam(data);
    start('OpenURL('+encodeURI(url)+')');
    //window.open(url);

}
function closeUser() {
    start('WebbrowserExit');
}


/**
 * 将对象转换为url字符参数
 */
String.prototype.replaceAll = function(s1,s2){
    return this.replace(new RegExp(s1,"gm"),s2);
}
function SetUrlParam(object) {
    var urlParam = "";
    if(object) {
        // 遍历属性值
        for(var p in object) {
            // 不传递方法参数
            if(typeof(object[p]) == "function"||typeof(object[p]) == "object") continue;
            // 不传递空值参数
            if(object[p] == null || object[p] == "") continue;
            urlParam += p + "=" + object[p] + "&";
        }
    }
    // 去掉末位的&
    urlParam = urlParam.substring(0, urlParam.length - 1);
    return urlParam;
};

/**
 * 将Url转换为json参数
 */
function GetUrlParam() {
    var jsonData= {};
    var reg = new RegExp("(^|&)" + name+ "=([^&]*)(&|$)");
    var r = window.location.search.substr(1);
    var data=r.split("&");
    for(var p in data) {
        // 不传递方法参数
        if(typeof(data[p]) == "function") continue;
        // 不传递空值参数
        if(data[p] == null || data[p] == "") continue;
        var string=data[p].split("=");
        var ret=decodeURI(string[1]);
        if(ret.indexOf("%")>=0) ret=decodeURI(unescape(string[1]));
        jsonData[string[0]]=ret;
    }
    return jsonData;
};

function GetQueryString(name,noLD) {
    if(noLD){
        var reg = new RegExp("(^|&)" + name.toLowerCase() + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).toLowerCase().match(reg);
    }else{
        var reg = new RegExp("(^|&)" + name+ "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
    }


    if (r != null){
        var ret=decodeURI(r[2]);
        if(ret.indexOf("%")>=0) ret=decodeURI(unescape(r[2]));
        return ret;
    }
    return null;
}


//JS操作cookies方法!
//写cookies
function setCookie(name,value)
{
    var Days = 30;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}

function getCookie(name)
{
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
    if(arr=document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return null;
}